#ifndef __SBI_H__
#define __SBI_H__

#include "types.h"

// SBI Extension IDs
#define SBI_EXT_BASE             0x10
#define SBI_EXT_TIME             0x54494D45
#define SBI_EXT_IPI              0x735049
#define SBI_EXT_RFENCE           0x52464E43
#define SBI_EXT_HSM              0x48534D
#define SBI_EXT_SRST             0x53525354

// SBI Base Extension Function IDs  
#define SBI_EXT_BASE_GET_SPEC_VERSION      0x0
#define SBI_EXT_BASE_GET_IMPL_ID           0x1
#define SBI_EXT_BASE_GET_IMPL_VERSION      0x2
#define SBI_EXT_BASE_PROBE_EXT             0x3
#define SBI_EXT_BASE_GET_MVENDORID         0x4
#define SBI_EXT_BASE_GET_MARCHID           0x5
#define SBI_EXT_BASE_GET_MIMPID            0x6

// SBI Timer Extension Function IDs
#define SBI_EXT_TIME_SET_TIMER             0x0

// SBI IPI Extension Function IDs
#define SBI_EXT_IPI_SEND_IPI               0x0

// SBI HSM Extension Function IDs
#define SBI_EXT_HSM_HART_START             0x0
#define SBI_EXT_HSM_HART_STOP              0x1
#define SBI_EXT_HSM_HART_GET_STATUS        0x2

// SBI System Reset Extension Function IDs
#define SBI_EXT_SRST_SYSTEM_RESET          0x0

// SBI Return Error Codes
#define SBI_SUCCESS               0
#define SBI_ERR_FAILED           -1
#define SBI_ERR_NOT_SUPPORTED    -2
#define SBI_ERR_INVALID_PARAM    -3
#define SBI_ERR_DENIED           -4
#define SBI_ERR_INVALID_ADDRESS  -5
#define SBI_ERR_ALREADY_AVAILABLE -6

struct sbiret {
    long error;
    long value;
};

struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
                        unsigned long arg1, unsigned long arg2,
                        unsigned long arg3, unsigned long arg4,
                        unsigned long arg5);

// Helper functions
void sbi_set_timer(uint64 stime_value);
void sbi_send_ipi(unsigned long hart_mask, unsigned long hart_mask_base);
void sbi_remote_fence_i(unsigned long hart_mask, unsigned long hart_mask_base);
void sbi_shutdown(void);
struct sbiret sbi_hart_start(unsigned long hartid, unsigned long start_addr, unsigned long opaque);

// Console functions (legacy)
void sbi_console_putchar(int ch);
int sbi_console_getchar(void);

#endif /* __SBI_H__ */
